package Program.AdminUI;

import Program.Information.User;
import Program.SqlTool.AdminDAO;
import Program.SqlTool.UserDAO;
import Program.Tool.Popup;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.ArrayList;
import java.util.Calendar;

public class OverheadPaneSon2 extends JPanel {
    private JButton generateButton, clearButton, boxRefreshBtn;
    private JComboBox<String> payBox;
    private ArrayList<User> list;
    //日期输入框 格式: xxxx/x 或者 xxxx/xx
    private JTextField dateField;
    //水用量输入框  立方米为单位
    private JTextField waterField;
    //电用量输入框  度为单位
    private JTextField electricField;
    //天然气量输入框  方为单位
    private JTextField gasField;
    //停车天数输入框  天为单位
    private JTextField parkDayField;
    private Font font1 = new Font("微软雅黑", Font.BOLD, 20);
    private Font font2 = new Font("楷体", Font.PLAIN, 16);
    //用户
    private User user;

    public OverheadPaneSon2() {
        super(null);
        setBounds(0, 0, 900, 600);
        initField();
        initJButton();
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawImage(new ImageIcon("Application_images\\userBG2.png").getImage(), 0, 0, this.getWidth(), this.getHeight(), this);
    }

    private void initField() {
        //日期标签
        JLabel dateJLabel = new JLabel("日  期");
        dateJLabel.setBounds(260, 60, 100, 50);
        dateJLabel.setFont(font1);
        this.add(dateJLabel);

        //日期日期输入框
        dateField = new JTextField();
        dateField.setBounds(420, 65, 120, 50);
        dateField.setBorder(BorderFactory.createLineBorder(Color.black, 2));
        dateField.setFont(font1);
        dateField.setToolTipText("格式: xxxx/x 或者 xxxx/xx");
        this.add(dateField);


        //选择用户标签
        JLabel userJLabel = new JLabel("选择用户");
        userJLabel.setBounds(250, 140, 100, 50);
        userJLabel.setFont(font1);
        this.add(userJLabel);
        //添加用户选择下拉框
        payBox = initBox();
        payBox.addItemListener(getItemListener());
        this.add(payBox);

        //水用量标签
        JLabel waterJLabel = new JLabel("水用量");
        waterJLabel.setBounds(260, 210, 100, 50);
        waterJLabel.setFont(font1);
        this.add(waterJLabel);
        //水用量输入框  立方米为单位
        waterField = new JTextField();
        waterField.setBounds(400, 215, 200, 36);
        waterField.setBorder(BorderFactory.createLineBorder(Color.black, 2));
        waterField.setFont(font1);
        this.add(waterField);
        //水用量的单位标签
        JLabel waterUintJLabel = new JLabel("立方米");
        waterUintJLabel.setBounds(630, 210, 100, 50);
        waterUintJLabel.setFont(font1);
        this.add(waterUintJLabel);

        //电用量标签
        JLabel electricJLabel = new JLabel("电用量");
        electricJLabel.setBounds(260, 280, 100, 50);
        electricJLabel.setFont(font1);
        this.add(electricJLabel);
        //电用量输入框  度为单位
        electricField = new JTextField();
        electricField.setBounds(400, 290, 200, 36);
        electricField.setBorder(BorderFactory.createLineBorder(Color.black, 2));
        electricField.setFont(font1);
        this.add(electricField);
        //电用量的单位标签
        JLabel electricUintJLabel = new JLabel("度");
        electricUintJLabel.setBounds(630, 280, 100, 50);
        electricUintJLabel.setFont(font1);
        this.add(electricUintJLabel);

        //天然气量标签
        JLabel gasJLabel = new JLabel("天然气用量");
        gasJLabel.setBounds(255, 350, 100, 50);
        gasJLabel.setFont(font1);
        this.add(gasJLabel);
        //天然气量输入框  方为单位
        gasField = new JTextField();
        gasField.setBounds(400, 355, 200, 36);
        gasField.setBorder(BorderFactory.createLineBorder(Color.black, 2));
        gasField.setFont(font1);
        this.add(gasField);
        //天然气量的单位标签
        JLabel gasUintJLabel = new JLabel("方");
        gasUintJLabel.setBounds(630, 350, 100, 50);
        gasUintJLabel.setFont(font1);
        this.add(gasUintJLabel);

        //停车天数标签
        JLabel parkJLabel = new JLabel("停车天数");
        parkJLabel.setBounds(255, 420, 100, 50);
        parkJLabel.setFont(font1);
        this.add(parkJLabel);
        //停车天数输入框  方为单位
        parkDayField = new JTextField();
        parkDayField.setBounds(400, 425, 200, 36);
        parkDayField.setBorder(BorderFactory.createLineBorder(Color.black, 2));
        parkDayField.setFont(font1);
        this.add(parkDayField);
        //停车天数的单位标签
        JLabel parkUintJLabel = new JLabel("天");
        parkUintJLabel.setBounds(630, 420, 100, 50);
        parkUintJLabel.setFont(font1);
        this.add(parkUintJLabel);
    }

    //用户选择下拉框
    public JComboBox<String> initBox() {
        JComboBox<String> box = new JComboBox<String>();
        box.setBounds(420, 145, 140, 36);
        box.setFont(font2);
        box.setBackground(Color.white);
        box.setBorder(null);
        list = AdminDAO.selectUsers();
        box.addItem(" ---请选择--- ");
        for (User user : list) {
            // 可用状态的用户才能用于生成业单
            if (user.getState().equals("1")) {
                box.addItem(user.getUserName());
            }
        }
        return box;
    }

    //初始化按钮
    private void initJButton() {
        //列表刷新按钮
        boxRefreshBtn = new JButton();
        boxRefreshBtn.setBounds(580, 145, 32, 32);
        boxRefreshBtn.setFocusPainted(false);
        boxRefreshBtn.setBorder(null);
        boxRefreshBtn.setContentAreaFilled(false);
        boxRefreshBtn.setToolTipText("列表刷新");
        boxRefreshBtn.setIcon(new ImageIcon("Application_images\\boxRefresh1.png"));
        boxRefreshBtn.setRolloverIcon(new ImageIcon("Application_images\\boxRefresh2.png"));
        boxRefreshBtn.setPressedIcon(new ImageIcon("Application_images\\boxRefresh3.png"));
        boxRefreshBtn.setCursor(new Cursor(Cursor.HAND_CURSOR));

        //生成按钮
        generateButton = new JButton("生成");
        generateButton.setBounds(330, 500, 90, 40);
        generateButton.setBorder(null);
        generateButton.setFocusPainted(false);
        generateButton.setForeground(Color.white);
        generateButton.setBackground(new Color(30, 180, 64));
        generateButton.setEnabled(true);
        generateButton.setFont(font1);
        generateButton.setCursor(new Cursor(Cursor.HAND_CURSOR));

        //清空按钮
        clearButton = new JButton("清空");
        clearButton.setBounds(510, 500, 90, 40);
        clearButton.setBorder(null);
        clearButton.setFocusPainted(false);
        clearButton.setForeground(Color.white);
        clearButton.setBackground(new Color(130, 100, 64));
        clearButton.setFont(font1);
        clearButton.setCursor(new Cursor(Cursor.HAND_CURSOR));


        ActionListener listener = getAcListener();

        boxRefreshBtn.addActionListener(listener);
        clearButton.addActionListener(listener);
        generateButton.addActionListener(listener);

        this.add(boxRefreshBtn);
        this.add(generateButton);
        this.add(clearButton);
    }

    //获取下拉框的监听器
    public ItemListener getItemListener() {
        ItemListener itemListener = new ItemListener() {
            @Override
            public void itemStateChanged(ItemEvent e) {
                if (e.getStateChange() == e.SELECTED) {
                    if (payBox.getSelectedIndex() == 0) {
                        generateButton.setBackground(new Color(30, 180, 64));
                        generateButton.setEnabled(true);
                    }else {
                        String selectedItem = (String) payBox.getSelectedItem();
                        user = UserDAO.selectUser(selectedItem);
                        if (user.getState().equals("0")) {
                            generateButton.setBackground(Color.gray);
                            generateButton.setEnabled(false);
                        } else {
                            generateButton.setBackground(new Color(30, 180, 64));
                            generateButton.setEnabled(true);
                        }
                    }
                }
            }
        };
        return itemListener;
    }

    //获取按钮监听器
    private ActionListener getAcListener() {
        ActionListener listener = new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (e.getSource() == boxRefreshBtn) {
                    refreshBox();
                } else if (e.getSource() == clearButton) {
                    clearField();
                }else if (e.getSource() == generateButton){
                    String waterUsage = waterField.getText();
                    String electricUsage = electricField.getText();
                    String gasUsage = gasField.getText();
                    String parkDay = parkDayField.getText();
                    String day = dateField.getText();
                    if (waterUsage.length()*electricUsage.length()*gasUsage.length()*parkDay.length() == 0){
                        Popup.showJDialog("请完善信息");
                    }else if (payBox.getSelectedIndex() == 0){
                        Popup.showJDialog("请选择用户");
                    } else {
                        int userID = user.getUserID();
                        if (AdminDAO.checkDate(userID,day)){
                            Popup.showJDialog("<html>生成失败，用户该日期的账单已存在，请勿重复生成</html>");
                        }else {
                            boolean b = AdminDAO.addUsage(userID,waterUsage,electricUsage,gasUsage,parkDay,day);
                            if (b){
                                Popup.showJDialog("生成成功");
                            }else {
                                Popup.showJDialog("生成失败");
                            }
                            refreshBox();
                            clearField();
                        }
                    }
                }
            }
        };
        return listener;
    }

    //清空输入框
    private void clearField(){
        waterField.setText("");
        electricField.setText("");
        gasField.setText("");
        parkDayField.setText("");
    }

    //重新加载下拉框
    private void refreshBox(){
        remove(payBox);
        repaint();
        updateUI();
        payBox = initBox();
        add(payBox);
    }

    //获取时间
    private String getDateStr() {
        //获取时间对象
        Calendar instance = Calendar.getInstance();
        //3.获取属性
        int year = instance.get(Calendar.YEAR);
        int month = instance.get(Calendar.MONTH) + 1;//Calendar的月份值是0-11
        return year + "/" + month;
    }
}
